package com.zmn.uac.business.impl.channel;

import com.alibaba.fastjson.JSONObject;
import com.zmn.base.channel.common.dro.ChannelDRO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.saas.common.dro.app.AppInfoDRO;
import com.zmn.saas.dubbo.dto.app.AppDRO;
import com.zmn.uac.business.interfaces.channel.ChannelBService;
import com.zmn.uac.client.AppClient;
import com.zmn.uac.client.ChannelListClient;
import com.zmn.uac.common.constant.RedisKeyConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 类描述：渠道业务实现
 *
 * @author xujie
 * @since 2021/06/17 14:12
 */
@Slf4j
@Service
public class ChannelBServiceImpl implements ChannelBService {

    private static final String TAG = "渠道业务实现";

    @Resource
    RedisTemplate<String, String> redisTemplate;

    @Resource
    ChannelListClient channelListClient;

    @Resource
    AppClient appClient;

    @Override
    public String getNameByChannelId(Integer channelId) {
        String key = RedisKeyConstant.getChannelNameKey(channelId);
        String value = redisTemplate.opsForValue().get(key);
        if (StringUtil.isNotBlank(value)) {
            return value;
        }
        ChannelDRO channel = channelListClient.getByChannelId(channelId);
        if (Objects.nonNull(channel)) {
            redisTemplate.opsForValue().set(key, channel.getName(), 1, TimeUnit.HOURS);
            return channel.getName();
        }

        return GlobalConsts.SPLITER_EMPTY;
    }


    @Override
    public AppDRO getAppByChannelId(Integer channelId) {
        String key = RedisKeyConstant.getChannelAppKey(channelId);
        String value = redisTemplate.opsForValue().get(key);
        try {
            if (StringUtil.isNotBlank(value)) {
                return JSONObject.parseObject(value, AppDRO.class);
            }
            AppDRO app = appClient.getAppByChannelId(channelId);
            if (Objects.nonNull(app)) {
                value = JSONObject.toJSONString(app);
                redisTemplate.opsForValue().set(key, value, 1, TimeUnit.HOURS);
                return app;
            }

        } catch (Exception e) {
            log.error("[{}], 获取应用配置异常channelId:{}, msg:{}", TAG, channelId, e.getMessage(), e);
        }
        return null;
    }

    @Override
    public AppInfoDRO getShowAppByChannelIdOrShopId(Integer channelId, Integer shopId) {
        String key = RedisKeyConstant.getThirdAppKey(channelId + ":" + shopId);
        // 当店铺id不为空的时候，只需要通过店铺id获取相关的配置信息，将渠道id设置为null，解决一个渠道下存在多个店铺的原因
        if (NumberUtil.isNullOrZero(shopId)) {
            key = RedisKeyConstant.getThirdAppKey(channelId.toString());
        } else {
            channelId = null;
        }
        String value = redisTemplate.opsForValue().get(key);
        if (StringUtil.isNotBlank(value)) {
            return JSONObject.parseObject(value, AppInfoDRO.class);
        }
        try {
            AppInfoDRO appInfo = appClient.getShowAppByChannelIdOrShopId(channelId, shopId);
            if (appInfo != null) {
                value = JSONObject.toJSONString(appInfo);
                redisTemplate.opsForValue().set(key, value, 1, TimeUnit.HOURS);
                return appInfo;
            }
        } catch (Exception e) {
            log.error("[{}], 获取应用配置异常channelId:{}, msg:{}", TAG, channelId, e.getMessage(), e);
        }
        return null;
    }


    @Override
    public AppDRO getAppInfoByShopId(Integer shopId) {
        String key = RedisKeyConstant.getShopAppKey(shopId);
        String value = redisTemplate.opsForValue().get(key);

        // 如果有直接从缓存中返回 否则从saas获取
        if (StringUtil.isNotBlank(value)) {
            return JSONObject.parseObject(value, AppDRO.class);
        }
        try {
            // 获取店铺配置三方账号app信息
            com.zmn.saas.common.dro.app.AppDRO app = appClient.getAppInfoByShopId(shopId);
            if (Objects.nonNull(app)) {
                AppDRO oldAppDRO = BeanMapper.map(app, AppDRO.class);
                oldAppDRO.setThirdAppId(app.getThirdAppId());
                oldAppDRO.setThirdAppSecret(app.getThirdAppSecret());
                oldAppDRO.setThirdAppSecretPublic(app.getThirdAppSecretPublic());

                value = JSONObject.toJSONString(oldAppDRO);
                redisTemplate.opsForValue().set(key, value, 1, TimeUnit.HOURS);

                return oldAppDRO;
            }

        } catch (Exception e) {
            log.error("[{}], 获取应用配置异常shopId:{}, msg:{}", TAG, shopId, e.getMessage(), e);
        }
        return null;
    }
}
